// webpack/webpack.dev.config.js
//webpack 配置文件
const path = require('path');

//使用webpack-node-externals来排除不需要打包的模块，因为 node 端会自动载入这些包，可以让打包的文件更小。
const nodeExternals = require('webpack-node-externals');

const webpack = require('webpack');
const resolvePath = (pathstr) => path.resolve(__dirname, pathstr);

process.env.BABEL_ENV = 'node';//设置 babel 的运行环境

const isProd=process.env.NODE_ENV==='production';

module.exports = {
  mode: process.env.NODE_ENV,
  target: 'node', // in order to ignore built-in modules like path, fs, etc.
  entry: resolvePath('../src/server/app/index.js'),//入口文件
  output: {
    filename: 'app.js',
    path: resolvePath('../dist/server')
  },
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
  module: {
    rules: [{
      test: /\.jsx?$/,
      loader: 'babel-loader',
      exclude: /node_modules/
    },{
      test: /.(png|jpg|gif)$/,
      use: {
        loader: 'file-loader',
        options: {
          emitFile: false,
          name: isProd ? 'img/[name].[hash:8].[ext]' : 'img/[name].[ext]',
          publicPath: isProd ? '/' : 'http://localhost:9002'
        }
      }
    }]
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env': { NODE_ENV: `"${process.env.NODE_ENV}"`},
      '__IS_PROD__':isProd,
      '__SERVER__': true
    })
  ],
  resolve: {
    alias: {
      //定义dist 目录别名，方便导入模块
      '@dist': path.resolve(__dirname,'../dist')
    }
  }
};
